package com.lovepick.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lovepick.R;
import com.lovepick.bean.Friend;
import com.lovepick.util.UserStatus;

public class FriendAdapter extends BaseAdapter
{
	private Context mContext;
	private LayoutInflater mInflater;
	private List<Friend> mFriends = null;

	public FriendAdapter(Context context, List<Friend> friends)
	{
		this.mContext = context;
		this.mFriends = friends;
		this.mInflater = LayoutInflater.from(mContext);
	}


	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return mFriends.size();
	}

	@Override
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return mFriends.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		Friend friend = mFriends.get(position);

		if (null == convertView)
		{
			convertView = mInflater.inflate(R.layout.friend_item, null);
			viewHolder = new ViewHolder();
			viewHolder.nickName = (TextView) convertView
					.findViewById(R.id.friend_nickname);
			viewHolder.avatar = (ImageView) convertView
					.findViewById(R.id.friend_avatar);
			viewHolder.status = (ImageView) convertView
					.findViewById(R.id.friend_status);
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.nickName.setText(friend.getNickName());

		String status = friend.getStatus();

		if (UserStatus.ONLINE.equals(status))
		{
			viewHolder.status.setImageDrawable(mContext.getResources()
					.getDrawable(R.drawable.sys_status_online));
		}
		else if (UserStatus.OFFLINE.equals(status))
		{
			viewHolder.status.setImageDrawable(mContext.getResources()
					.getDrawable(R.drawable.sys_status_offline));
		}
		else if (UserStatus.BUSY.equals(status))
		{
			viewHolder.status.setImageDrawable(mContext.getResources()
					.getDrawable(R.drawable.sys_status_busy));
		}
		else if (UserStatus.AWAY.equals(status))
		{
			viewHolder.status.setImageDrawable(mContext.getResources()
					.getDrawable(R.drawable.sys_status_away));
		}

		viewHolder.avatar.setImageDrawable(mContext.getResources().getDrawable(
				R.drawable.avatar));

		return convertView;
	}

	static class ViewHolder
	{
		public ImageView avatar;
		public ImageView status;
		public TextView nickName;
	}

}
